package com.jctrip.hotel.source.xxl.hworld.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.hworld.HworldService;
import com.jctrip.hotel.source.service.hworld.HworldTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-09 21:35
 * @Description:
 */
@Slf4j
public class SyncHworldRoomRepairTaskThread implements Runnable {

    private final String appId;

    private final String key;
    private final String memberLevel;

    public SyncHworldRoomRepairTaskThread(String appId, String key, String memberLevel) {
        this.appId = appId;
        this.key = key;
        this.memberLevel = memberLevel;
    }

    @Override
    public void run() {
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
        HworldTaskService hworldTaskService = BeanContext.getApplicationContext().getBean(HworldTaskService.class);
        HworldService hworldService = BeanContext.getApplicationContext().getBean(HworldService.class);

        while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_HWORLD_ROOM_REPAIR_LIST) > 0) {
            try {
                String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_HWORLD_ROOM_REPAIR_LIST);
                if (StringUtils.isBlank(hotelId)) {
                    continue;
                }
                Date start = new Date();
                Date end = DateUtils.addDays(start, 1);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                hworldTaskService.syncHworldRoomRepair(appId, key, hworldService.parseHotelId(hotelId),
                        simpleDateFormat.format(start), simpleDateFormat.format(end), memberLevel);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}